<?php

class Media_Callback
{

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return boolean
	 */
	public static function valid(WSB_DirectoryIterator $iterator)
	{
		return !$iterator->isDot();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function icon(WSB_DirectoryIterator $iterator)
	{
		if($iterator->isDot()) {
			$icon = '';
		} elseif($iterator->isFile()) {
			$icon = WSB_Icon::file();
		} else {
			$icon = WSB_Icon::folder();
		}
		return $icon;
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function type(WSB_DirectoryIterator $iterator)
	{
		return $iterator->getExtension();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function name(WSB_DirectoryIterator $iterator)
	{
		return $iterator->getFilename();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function trashName(WSB_DirectoryIterator $iterator)
	{
		return $iterator->getFilename();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function perm(WSB_DirectoryIterator $iterator)
	{
		$str = '';
		$str .= ($iterator->getFileInfo()->isReadable()) ? 'r' : '-';
		$str .= ($iterator->getFileInfo()->isWritable()) ? 'w' : '-';
		return $str;
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function links(WSB_DirectoryIterator $iterator)
	{
		$links = array();

		// Datei
		if($iterator->isFile()) {
			// Anzeigen
			array_push($links, sprintf('<a target="_blank" href="%s">%s</a>', WSB_URL . $iterator->getRelativePath(), WSB_Icon::view()));

			// Ausschneiden
			$icon = WSB_Icon::cut();
			$href = Media_Url::cutUrl($iterator->getRelativeMediaPath());
			array_push($links, WSB_Controller_Url::getHtmlLink($icon, $href));

			// Loeschen
			$icon = WSB_Icon::delete();
			$href = Media_Url::fileDeleteUrl($iterator->getRelativeMediaPath());
			array_push($links, WSB_Controller_Url::getHtmlLink($icon, "javascript:wsbYesNoDialog('".$href."','".$iterator->getFilename()."')"));

		// Ordner, aber kein "." oder ".."
		} elseif(!$iterator->isDot()) {

			if($iterator->isEmpty()) {
				// Loeschen
				$icon = WSB_Icon::delete();
				$href = Media_Url::fileDeleteUrl($iterator->getRelativeMediaPath());
				array_push($links, WSB_Icon::cleargif());
				array_push($links, WSB_Icon::cleargif());
				array_push($links, WSB_Controller_Url::getHtmlLink($icon, "javascript:wsbYesNoDialog('".$href."','".$iterator->getFilename()."')"));
			}

			// Session Objekt neu erzeugen (da statische Methode)
			$session = new Zend_Session_Namespace('Media_FileController');
			// Einfuegen
			if($session->cut) {
				$icon = WSB_Icon::child();
				$href = Media_Url::pasteUrl($iterator->getRelativeMediaPath());
				array_push($links, WSB_Controller_Url::getHtmlLink($icon, $href));
			} else {
			//	array_push($links, WSB_Icon::cleargif());
			}
		}
		return '<span style="white-space:nowrap;">'.implode('', $links).'</span>';
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return boolean
	 */
	public static function trashValid(WSB_DirectoryIterator $iterator)
	{
		return !$iterator->isDot();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function trashLinks(WSB_DirectoryIterator $iterator)
	{
		$links = array();

		// Datei
		if($iterator->isFile()) {
			// Anzeigen
			array_push($links, sprintf('<a target="_blank" href="%s">%s</a>', WSB_URL . $iterator->getRelativePath(), WSB_Icon::view()));

			// Loeschen
			$icon = WSB_Icon::undo();
			$href = Media_Url::trashRestoreUrl($iterator->getRelativeTrashPath());
			array_push($links, WSB_Controller_Url::getHtmlLink($icon, $href));

		}
		return '<span style="white-space:nowrap;">'.implode('', $links).'</span>';
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return boolean
	 */
	public static function fckeditorValid(WSB_DirectoryIterator $iterator)
	{
		/*
		$valid = true;
		if($iterator->isDot()) {
			if($iterator->getFilename() == '.') {
				$valid = false;
			}
			if($iterator->getFilename() == '..') {
				if($iterator->getRelativeMediaPath() == '..') {
					$valid = false;
				}
			}
		}
		return $valid;
		*/
		return !$iterator->isDot();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function fckeditorName(WSB_DirectoryIterator $iterator)
	{
		if($iterator->isDir()) {
			if($iterator->getFilename() == '..') {
				return WSB_Controller_Url::getHtmlLink('..', Media_Url::fileFckeditorUrl($iterator->getParentFileInfo()->getParentFileInfo()->getRelativeMediaPath()));
			} else {
				return WSB_Controller_Url::getHtmlLink($iterator->getFilename(), Media_Url::fileFckeditorUrl($iterator->getRelativeMediaPath()));
			}
		} else {
			return $iterator->getFilename();
		}
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function fckeditorLinks(WSB_DirectoryIterator $iterator)
	{
		if($iterator->isFile()) {
			$session = new Zend_Session_Namespace('Media_FileController');
			$links = array();
			// Image
			if(($session->filter == 'image') && in_array($iterator->getExtension(), array('jpg', 'gif', 'png'))) {
				$size = getimagesize($iterator->getPathname());
				$htmlTag = htmlspecialchars(sprintf('<img src="%s" width="%s" height="%s" alt="" />', $iterator->getRelativePath(), $size[0], $size[1]));
				array_push($links, sprintf('<a href="javascript:insertHtml(\'%s\');">%s</a>', $htmlTag, WSB_Icon::add()));
			// File
			} elseif($session->filter == 'file') {
				$htmlTag = htmlspecialchars(sprintf('<a href="%s">%s</a>', $iterator->getRelativePath(), $iterator->getRelativePath()));
				array_push($links, sprintf('<a href="javascript:insertHtml(\'%s\');">%s</a>', $htmlTag, WSB_Icon::add()));
			}
			return '<span style="white-space:nowrap;">'.implode('', $links).'</span>';
		}
		return '';
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return boolean
	 */
	public static function browserValid(WSB_DirectoryIterator $iterator)
	{
		/*
		$valid = true;
		if($iterator->isDot()) {
			if($iterator->getFilename() == '.') {
				$valid = false;
			}
			if($iterator->getFilename() == '..') {
				if($iterator->getRelativeMediaPath() == '..') {
					$valid = false;
				}
			}
		}
		return $valid;
		*/
		return !$iterator->isDot();
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function browserName(WSB_DirectoryIterator $iterator)
	{
		if($iterator->isDir()) {
			if($iterator->getFilename() == '..') {
				return WSB_Controller_Url::getHtmlLink('..', Media_Url::fileBrowserUrl($iterator->getParentFileInfo()->getParentFileInfo()->getRelativeMediaPath()));
			} else {
				return WSB_Controller_Url::getHtmlLink($iterator->getFilename(), Media_Url::fileBrowserUrl($iterator->getRelativeMediaPath()));
			}
		} else {
			return $iterator->getFilename();
		}
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function browserLinks(WSB_DirectoryIterator $iterator)
	{
		if($iterator->isFile()) {

			$session = new Zend_Session_Namespace('Media_FileController');
			$elementId = $session->opener['elementId'];
			$selectId = $session->opener['selectId'];

			$session = new Zend_Session_Namespace('Media_FileController');
			$links = array();
			// Image
			if(($session->filter == 'image') && in_array($iterator->getExtension(), array('jpg', 'gif', 'png'))) {
				$size = getimagesize($iterator->getPathname());
				$htmlTag = htmlspecialchars(sprintf('<img src="%s" width="%s" height="%s" alt="" />', $iterator->getRelativePath(), $size[0], $size[1]));
				array_push($links, sprintf('<a href="javascript:opener.addOption(\'%s\',\'%s\',\'%s\',\'%s\');">%s</a>', $selectId, $elementId, $iterator->getRelativePath(), $iterator->getRelativePath(), WSB_Icon::add()));
			// File
			} elseif($session->filter == 'file') {
				$htmlTag = htmlspecialchars(sprintf('<a href="%s">%s</a>', $iterator->getRelativePath(), $iterator->getRelativePath()));
				array_push($links, sprintf('<a href="javascript:opener.addOption(\'%s\',\'%s\',\'%s\',\'%s\');">%s</a>', $selectId, $elementId, $iterator->getRelativePath(), $iterator->getRelativePath(), WSB_Icon::add()));
			}
			return '<span style="white-space:nowrap;">'.implode('', $links).'</span>';
		}
		return '';
	}

	/**
	 * @param WSB_DirectoryIterator $iterator
	 * @return string
	 */
	public static function size(WSB_DirectoryIterator $iterator)
	{
		return WSB_Div::getHumanReadableFileSize($iterator->getFileInfo()->getSize());
	}

}
